package com.person.monitor;

import android.content.Context;
import android.content.Intent;

import java.util.ArrayList;
import java.util.List;

public class OpenXiaomiProcess extends BaseProcess{

    private static String keyValue = "Pro_128141";

    private boolean needOpen = true;

    public OpenXiaomiProcess(Context context, String name) {
        super(context, name);
    }

    @Override
    public void processMonitor(List<MonitorInfo> list) {
        if(list.size() < 2 ) return;

        MonitorInfo last = list.get(1);
        MonitorInfo now = list.get(0);

        if(needOpen){
            needOpen = false;
            openXiaomiCar();
        }else if(!last.wifiContainKey(keyValue) && now.wifiContainKey(keyValue)){
            openXiaomiCar();
        }
    }

    //com.mi.car.mobile/com.mi.car.mobile.LauncherActivity
    private void openXiaomiCar(){
        LogcatHelper.getInstance().printLog("openXiaomiCar");

        Intent intent = new Intent();
        intent.setClassName("com.mi.car.mobile", "com.mi.car.mobile.LauncherActivity");
        intent.setFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY|Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            context.startActivity(intent);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /*
  Connection bindings to services:
  * ConnectionRecord{d9295a5 u0 CR com.mi.car.mobile/com.xiaomi.innericcoadigitalkey.dkf.InnerICCOADKService:@e8279c}
    binding=AppBindRecord{3aedb7f com.mi.car.mobile/com.xiaomi.innericcoadigitalkey.dkf.InnerICCOADKService:com.mi.car.mobile}
    conn=android.os.BinderProxy@e8279c flags=0x1
  * ConnectionRecord{5354bf4 u0 CR com.mi.car.mobile/com.mi.car.ble.library.BluetoothService:@28b99c7}
    binding=AppBindRecord{ea75924 com.mi.car.mobile/com.mi.car.ble.library.BluetoothService:com.mi.car.mobile}
    conn=android.os.BinderProxy@28b99c7 flags=0x1
  * ConnectionRecord{5d8f86e u0 CR com.mi.car.mobile/com.xiaomi.innericcoadigitalkey.dkf.InnerICCOADKService:@29ca3e9}
    binding=AppBindRecord{3aedb7f com.mi.car.mobile/com.xiaomi.innericcoadigitalkey.dkf.InnerICCOADKService:com.mi.car.mobile}
    conn=android.os.BinderProxy@29ca3e9 flags=0x1
  * ConnectionRecord{6d75d41 u0 CR com.mi.car.mobile/com.miui.tsmclient.service.ICCOARequestDKFService:@4031c28}
    binding=AppBindRecord{6cc89ae com.mi.car.mobile/com.miui.tsmclient.service.ICCOARequestDKFService:com.mi.car.mobile}
    conn=android.os.BinderProxy@4031c28 flags=0x1
  * ConnectionRecord{5d50b6c u0 CR com.mi.car.mobile/com.xiaomi.innericcoadigitalkey.dkf.InnerICCOADKService:@40d4c1f}
    binding=AppBindRecord{3aedb7f com.mi.car.mobile/com.xiaomi.innericcoadigitalkey.dkf.InnerICCOADKService:com.mi.car.mobile}
    conn=android.os.BinderProxy@40d4c1f flags=0x1
  * ConnectionRecord{24da897 u0 CR com.mi.car.mobile/com.xiaomi.spmstatsdk.service.SpmStatService:@5fcc716}
    binding=AppBindRecord{1eec411 com.mi.car.mobile/com.xiaomi.spmstatsdk.service.SpmStatService:com.mi.car.mobile}
    conn=android.os.BinderProxy@5fcc716 flags=0x1
  * ConnectionRecord{3890e9e u0 CR com.mi.car.mobile/com.getui.getuiflut.FlutterIntentService:@62374d9}
    binding=AppBindRecord{a88c79d com.mi.car.mobile/com.getui.getuiflut.FlutterIntentService:com.mi.car.mobile:pushservice}
    conn=android.os.BinderProxy@62374d9 flags=0x1
  * ConnectionRecord{c8d060e u0 CR com.mi.car.mobile/com.xiaomi.innericcoadigitalkey.dkf.InnerICCOADKService:@8c7e409}
    binding=AppBindRecord{3aedb7f com.mi.car.mobile/com.xiaomi.innericcoadigitalkey.dkf.InnerICCOADKService:com.mi.car.mobile}
    conn=android.os.BinderProxy@8c7e409 flags=0x1
  * ConnectionRecord{66fba37 u0 CR com.mi.car.mobile/com.xiaomi.innericcoadigitalkey.dkf.BleMonitorService:@a3b9b36}
    binding=AppBindRecord{ded2a2a com.mi.car.mobile/com.xiaomi.innericcoadigitalkey.dkf.BleMonitorService:com.mi.car.mobile}
     */

    public static void startService(Context context){
        LogcatHelper.getInstance().printLog("startXiaomiService");

        List<String> strings = new ArrayList<>();

        strings.add("com.xiaomi.innericcoadigitalkey.dkf.InnerICCOADKService");
        strings.add("com.mi.car.ble.library.BluetoothService");
        strings.add("com.xiaomi.innericcoadigitalkey.dkf.BleMonitorService");
        strings.add("com.miui.tsmclient.service.ICCOARequestDKFService");
        strings.add("com.xiaomi.spmstatsdk.service.SpmStatService");

        for(String str: strings) {
            startService(context, str);
        }
    }

    private static void startService(Context context, String className){
        Intent intent = new Intent();
        intent.setClassName("com.mi.car.mobile", className);
        try {
            context.startService(intent);
        }catch (Exception e){
            e.printStackTrace();
            MyLog.E("startService Fail:"+className);
        }
    }
}
